package com.mixpanel.android.util;

import android.content.Context;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.InputStream;
import java.io.InputStreamReader;

public class AssetsHelper {

    //已流的方式读取
    public static String getFromAssetsToString(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        InputStream is = null;
        InputStreamReader inputReader = null;
        BufferedReader bufReader = null;
        try {
            is = context.getAssets().open(fileName);
//            InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName));
            inputReader = new InputStreamReader(is);
            bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Throwable e) {
            L.e(e);
        } finally {
            SafeStream.close(is);
            SafeStream.close(bufReader);
            SafeStream.close(inputReader);
        }
        return stringBuilder.toString();
    }

    public static String getFromAssetsToMulitiLine(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        InputStream is = null;
        InputStreamReader inputReader = null;
        BufferedReader bufReader = null;
        try {
            is = context.getAssets().open(fileName);
//            InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName));
            inputReader = new InputStreamReader(is);
            bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
        } catch (Throwable e) {
            L.e(e);
        } finally {
            SafeStream.close(is);
            SafeStream.close(bufReader);
            SafeStream.close(inputReader);
        }
        return stringBuilder.toString();
    }
}

class SafeStream {

    public static void close(Closeable auto) {
        try {
            if (auto != null) {
                auto.close();
            }
        } catch (Throwable e) {
            L.e(e);
        }
    }
}
